// Package endpoint
// Title  			user.endpoint.go
// Description  	用户信息接口服务端
// Author  alittlexincan@163.com  2024/07/22 11:36
// Update  alittlexincan@163.com  2024/07/22 11:36
package endpoint

import (
	"context"
	_ "io/ioutil"
	_ "net/http"

	"github.com/go-kit/kit/endpoint"
	"xincan.com.cn/tarot-go/internal/app/tarot/user/dto"
	"xincan.com.cn/tarot-go/internal/app/tarot/user/service"
	rep "xincan.com.cn/tarot-go/internal/common/response"
	"xincan.com.cn/tarot-go/internal/components/middleware"
)

// MakeCreateUserEndpoint
// @Title    				MakeCreateUserEndpoint
// @Tags 					用户管理
// @Summary   		 		创建用户
// @Description   		 	创建用户，并返回用户详细信息
// @Auth      				alittlexincan@163.com         	2024/07/22 11:36
// @Update  				alittlexincan@163.com  			2024/07/22 11:36
// @Router 					/tarot/user [post]
// @Accept 					application/x-www-form-urlencoded
// @Produce 				application/json
// @Param 		name		query 		string 		true 	"用户名称" 	minlength(2) maxlength(20) 		required		default(张安)
// @Param 		password	query 		string 		true 	"用户密码" 	minlength(6) maxlength(20) 		required		default(123456)
// @Param 		role		query 		string 		true 	"用户角色" 	minlength(2) maxlength(20) 		required		default(admin)
// @Param 		type		query 		string 		true 	"用户类型" 	Enums(UNKNOWN,STUDENT,TEACHER) 	required		default(STUDENT)
// @Param 		status		query 		string 		true 	"用户状态" 	Enums(UNKNOWN,READY,EXCEPTION) 	required		default(READY)
// @Success 	200 		{object} 	response.Result{data=bool}												正常结果信息
// @Failure 	452 		{object} 	response.Result															参数格式错误
// @Failure 	500 		{object} 	response.Result															服务内部错误
func MakeCreateUserEndpoint(service service.UserInterface) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		model := request.(*dto.CreateUserDto)
		ep := createUserEndpoint(model, service)
		// 此处可以继续 endPoint 增加链式调用
		ep = middleware.ValidationObjectMiddleware()(ep)
		return ep(ctx, request)
	}
}

func createUserEndpoint(model *dto.CreateUserDto, svc service.UserInterface) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		bus, busErr := svc.CreateUser(model)
		if busErr != nil {
			return rep.Failure(err), nil
		}
		return rep.Success(bus), nil
	}
}

// MakeSearchUserListEndpoint
// @Title    				MakeSearchUserListEndpoint
// @Tags 					用户管理
// @Summary   		 		查询用户
// @Description   		 	根据用户ID查询用户信息，并返回用户详细信息
// @Auth      				alittlexincan@163.com         	2024/07/22 11:36
// @Update  				alittlexincan@163.com  			2024/07/22 11:36
// @Router 					/tarot/user [get]
// @Accept 					application/x-www-form-urlencoded
// @Produce 				application/json
// @Param 		id		 	query 		string 		false 	"用户ID" 	minlength(19) maxlength(19) 		required		default(1142681887794270208)
// @Success 	200 		{object} 	response.Result{data=[]vo.SearchUserListVo}							正常结果信息
// @Failure 	452 		{object} 	response.Result															参数格式错误
// @Failure 	454 		{object} 	response.Result															用户不存在
// @Failure 	500 		{object} 	response.Result															服务内部错误
func MakeSearchUserListEndpoint(service service.UserInterface) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		model := request.(*dto.SearchUserListDto)
		//if true {
		//	return exception.ErrUserNotFound, nil
		//}

		err = middleware.ValidationAttribute(ctx, "用户ID单值", model.Id, "required,numeric,v-num-len=19")

		if err != nil {
			return rep.Failure(err), nil
		}

		data, err := service.SearchUserList(model)
		if err != nil {
			return rep.Failure(err), nil
			//return exception.ErrUserNotFound, nil
		}

		return rep.Success(data), nil
	}
}
